﻿@page "/creator/plugins"
@inherits ListComponent<CmPlugin>
@attribute [UserMenu("插件管理", "far fa-plug", "plugins", 3)]

<UserLayout Title="插件列表">
    <CmsCard Class="cl-posts cl-plugin-list">
        <HeadTemplate>
            <CmsTitle Icon="far fa-plug" Title="插件列表" />
            <div style="display:flex;gap:10px;">
                <AntForm Id="query">
                    <AntSearch Id="Key" Placeholder="请输入关键字搜索" Value="@key" />
                </AntForm>
                <AntButton Name="添加插件" OnClick="@($"location='{Url.PluginForm}'")" />
            </div>
        </HeadTemplate>
        <ChildContent>
            <div class="cl-plugin-list-item">
                <div class="item">
                    <div style="font-weight:bold;">名称</div>
                    <div style="font-weight:bold;">分类</div>
                    <div style="font-weight:bold;">类型</div>
                    <div style="font-weight:bold;">价格</div>
                    <div style="font-weight:bold;">状态</div>
                    <div style="font-weight:bold;">创建时间</div>
                    <div style="font-weight:bold;">操作</div>
                </div>
            </div>
            @if (Result.TotalCount == 0)
            {
                <AntEmpty />
            }
            else
            {
                foreach (var item in Result.PageData)
                {
                    <div class="cl-plugin-list-item">
                        <div class="item">
                            <div>@item.Name</div>
                            <div>@item.Category</div>
                            <div>@item.Type</div>
                            <div>@item.Price</div>
                            <div><AntTag Color="">@item.Status</AntTag></div>
                            <div>@item.CreateTime.ToString("yyyy-MM-dd HH:mm")</div>
                            <div style="display:flex;">
                                <a href="@Url.GetPluginFormUrl(item.Id)" class="ant-tag ant-tag-blue">编辑</a>
                                @* <a href="@Url.GetPluginFormUrl(item.Id)" class="ant-tag ant-tag-red">删除</a> *@
                            </div>
                        </div>
                    </div>
                }
                <div style="padding:15px 0;text-align:center;">
                    <AntPagination TotalCount="Result.TotalCount" PageSize="Criteria.PageSize" PageIndex="Criteria.PageIndex" />
                </div>
            }
        </ChildContent>
    </CmsCard>
</UserLayout>

@code {
    private IPluginService Service;
    private string key;

    protected override async Task OnInitAsync()
    {
        if (CurrentUser == null)
            return;

        await base.OnInitAsync();
        Service = await CreateServiceAsync<IPluginService>();
        key = Context.Request.Get<string>("Key");
        Criteria.Parameters["Key"] = key;
        Criteria.SetQuery(nameof(CmPlugin.UserId), QueryType.Equal, CurrentUser.Id);
        Result = await Service.QueryPluginsAsync(Criteria);
    }
}